<?php


namespace App\Server;


use App\Models\Goods\Spec;
use App\Models\Goods\SpecValue;

class GoodsSpecService
{
    /**
     * 批量添加规格
     * @param $spec
     * @param $type_id
     */
    public function save($spec,$type_id){
        //循环对象$param['spec_value']
        $specData=[];
        foreach ($spec as $value){
            $specData[] = Spec::create([
                'type_id'=>$type_id,
                'spec_name'=>$value['name'],
            ]);
        }
        //批量入库
        return $specData;
    }

    /**
     * 批量添加规格值
     * @param $specRes
     * @param $spec
     * @param $cate_id
     * @return mixed
     */
    public function specValueSave($specRes,$spec,$cate_id){
        $specInfo=array_column($specRes,'id','spec_name');
//        dd($specInfo);
        //批量添加规格值
        $specValueData=[];
        foreach ($spec as $item){
            foreach ($item['value'] as $value){
                $specValueData[]=SpecValue::create([
                    'spec_id'=>$specInfo[$item['name']], //规格值id
                    'type_id'=>$cate_id, //类型id
                    'spec_value'=>$value, //规格值
                ]);
            }
        }
        return $specValueData;
    }
}
